/*
 * 文件名：MixEncryption.java 版权：Copyright by www.huawei.com 描述： 修改人：yinmx 810970932@qq.com
 * 修改时间：2015年8月28日 跟踪单号： 修改单号： 修改内容：
 */
package com.zcsy.commons.util;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

import sun.misc.BASE64Encoder;

/**
 * @Title: MixEncryption.java
 * @Package： com.zcsy.commons.util
 * @Description: 混合RSA和DES加密
 * @author yinmx 810970932@qq.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2015年8月28日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
public class MixEncryption {
    
    public static final String PUBLIC_KEY = "RSAPublicKey";// 公钥
    static RSAPublicKey publicKey = null;
    static RSAPrivateKey privateKey = null;
    //客户端加密
    public static Map<String, Object> Encrypt(String password, String loginName) {
        
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> key = new HashMap<String, Object>();
        String encryptData = null;
        String keyString = null;
        try {
            key = RSAEncryption.getKeys();
            publicKey = (RSAPublicKey)key.get("public");
            privateKey = (RSAPrivateKey)key.get("private");
            // 加密数据
            encryptData = DESEncryption.encrypt(password, loginName);
            // 加密DESkey
            keyString = RSAEncryption.encryptByPublicKey(loginName, publicKey);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        map.put("data", encryptData);
        map.put("key", keyString);
        return map;
    }

    public static Map<String, String> Decrypt(String password, String DESkey) {
        
        Map<String, String> map = new HashMap<String, String>();
        Map<String, Object> key = new HashMap<String, Object>();
        String decryptData = null;
        String data = null;
        try {
            // 解密DESkey
            data = RSAEncryption.decryptByPrivateKey(DESkey, privateKey);
            // 解密数据
            decryptData = DESEncryption.decrypt(password, data);
            map.put("data", decryptData);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        map.put("data", decryptData);
        
        return map;
    }

}
